package com.cf.service.impl;


import com.cf.dao.FocusDao;
import com.cf.dao.ItemDao;
import com.cf.dao.ItemManDao;
import com.cf.dao.RewardDao;
import com.cf.entity.Item;
import com.cf.entity.ItemMan;
import com.cf.entity.Reward;
import com.cf.service.ItemManService;
import com.cf.service.ItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 59899
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemDao itemDao;
    @Autowired
    private ItemManDao itemManDao;
    @Autowired
    private RewardDao rewardDao;
    @Autowired
    private FocusDao focusDao;

    @Override
    public int addMessToItem(Item item) {
        return itemDao.addMessToItem(item);
    }

    @Override
    public Item selectItemByIname(String iname) {
        return itemDao.selectItemByIname(iname);
    }


    @Override
    public int addStatusToItemById(long iid) {
        return itemDao.addStatusToItemById(iid);
    }

    @Override
    public int addTimeToItemById(Item item) {
        return itemDao.addTimeToItemById(item);
    }

    @Override
    public int deleteItemById(long iid) {
        return itemDao.deleteItemById(iid);
    }

    @Override
    public PageInfo<Item> selectAllLikeStatusPage(int pageNum, String status) {
        PageHelper.startPage(pageNum,5);
        List<Item> items = itemDao.selectAllLikeStatus(status);
        for (Item item : items) {
            ItemMan itemMan = itemManDao.selectItemManByIid(item.getIid());
            item.setItemMan(itemMan);
        }
        PageInfo<Item> itemPageInfo = new PageInfo<>(items);
        return itemPageInfo;
    }

    @Override
    public Item selectItemByIid(long iid) {
        Item item = itemDao.selectItemByIid(iid);
        if (item!=null){
            ItemMan itemMan = itemManDao.selectItemManByIid(iid);
            item.setItemMan(itemMan);
        }
        return item;
    }

    @Override
    public int updateItemStatusByIid(Map map) {
        return itemDao.updateItemStatusByIid(map);
    }

    @Override
    public List<Item> selectAllItem() {
        return null;
    }

    @Override
    public PageInfo<Item> selectItemByStatusAndCid(int pageNum, Map map) {
        PageHelper.startPage(pageNum,8);
        List<Item> items = itemDao.selectItemByStatusAndCid(map);
        DecimalFormat df = new DecimalFormat("#.00");
        for (Item item : items) {
            double bfb = item.getNmoney()/ item.getAmoney();
            item.setBfb(Double.parseDouble(df.format(bfb*100)));
        }
        PageInfo<Item> itemPageInfo = new PageInfo<>(items);
        return itemPageInfo;
    }

    @Override
    public PageInfo<Item> selectAllItemByPage(int pageNum) {
        PageHelper.startPage(pageNum,8);
        List<Item> items = itemDao.selectAllItem();
        DecimalFormat def = new DecimalFormat("#.00");
        for (Item item : items) {
            double bfb = item.getNmoney()/ item.getAmoney();
            item.setBfb(Double.parseDouble(def.format(bfb*100)));
        }
        PageInfo<Item> itemPageInfo = new PageInfo<>(items);
        return itemPageInfo;
    }

    @Override
    public int deleteLuoJi(long iid) {
        return itemDao.deleteLuoJi(iid);
    }

    @Override
    public Item selectRewardAndItem(long iid) {
        List<Reward> rewards = rewardDao.selectByItemid(iid);
        Item item = itemDao.selectItemByIid(iid);
        item.setRewards(rewards);
        double  percentage =  item.getNmoney()/item.getAmoney()*100;
        long round = Math.round(percentage);
        item.setPercentage(round);

        //查询项目总关注条数
        int gzcount = focusDao.selectCountByitemid(item.getIid());
        item.setGzcount(gzcount);

        //计算天数
        try {
            if (item.getIstart()==null||item.getIend()==null){
                item.setDaybetween(0);
            }else {
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                Date stime =sdf.parse(sdf.format(new Date()));
                Date etime =sdf.parse(sdf.format(item.getIend()));
                Calendar cal = Calendar.getInstance();
                cal.setTime(stime);
                long time1 = cal.getTimeInMillis();
                cal.setTime(etime);
                long time2 = cal.getTimeInMillis();
                long between_days=(time2-time1)/(1000*3600*24);
                item.setDaybetween(between_days);
            }
        }  catch (ParseException e) {
            e.printStackTrace();
        }
        return item;
    }

    @Override
    public List<Item> selectItemUserid(long userid) {
        List<Item> items = itemDao.selectItemUserid(userid);
        if (items.size() > 5) {//判断list长度
            List newList = items.subList(0, 4);//取前四条数据

            return newList;//返回新的list
        } else {
            return items;
        }
    }

    @Override
    public int updateStatusByIid(long iid, String notshow) {
        Map map = new HashMap();
        map.put("iid",iid);
        map.put("notshow",notshow);
        return itemDao.updateStatusByIid(map);
    }
}
